<?php
if ( phpversion() >= "4.2.0")
{  
    extract($_POST);  
    extract($_GET);  
    extract($_SERVER);  
}  

require_once "include/config.php";
require_once "include/functions.php"; 
require_once "include/google_translate.php";

$it_increase_replay_time = get_subtitle_increase_replay_time($current_subtitle_id);

$current_user__information = get_user_info();
//echo $current_user__information['id'];
//echo $current_subtitle_id;

if(can_edit_subtitle($current_user__information['id'],$current_subtitle_id)=="false" and get_user_group()!='1')
{
echo "you don't have permission to access this page";
die;
}

//echo $table_id."<br>";
//echo $id."<br>";
//echo $start."<br>";
//echo $end."<br>";
//echo $text."<br>";
//echo $save_x."<br>";
//echo $done_x."<br>";
//echo $done."<br>";
//echo convert_srt_time_to_seconds($start);
//echo convert_seconds_to_srt_time('451.457');

if(!empty($save_x) and !empty($save_y))
{
$saved_id = $id;
mysql_query("UPDATE `$table_id` SET `start` = '$start',
`end` = '$end',
`text` = '$text' WHERE `id` =$id LIMIT 1 ;");
}

if(!empty($done_x) and !empty($done_y))
{
mysql_query("UPDATE `$table_id` SET `done` = '1' WHERE `id` =$id LIMIT 1 ;");
}

if(!empty($not_done_x) and !empty($not_done_y))
{
mysql_query("UPDATE `$table_id` SET `done` = '0' WHERE `id` =$id LIMIT 1 ;");
}

if(!empty($add_after_x) and !empty($add_after_y))
{
$addafter = explode(':',$end);
$new_start = $addafter[0].':'.$addafter[1].':'.( $addafter[2] + 0.001);
$new_end = $addafter[0].':'.$addafter[1].':'.( $addafter[2] + 1.001);
//echo $new_start;
mysql_query("INSERT INTO `$table_id` (`text` ,`start` ,`end`  ,`done`)VALUES ('new line', '$new_start', '$new_end', '0');");
arrange_subtitle_lines_order($table_id);
}

if(!empty($add_before_x) and !empty($add_before_y))
{
$addbefore = explode(':',$start);
$new_start = $addbefore[0].':'.$addbefore[1].':'.( $addbefore[2] - 1.001);
$new_end = $addbefore[0].':'.$addbefore[1].':'.( $addbefore[2] - 0.001);
//echo $new_start;
mysql_query("INSERT INTO `$table_id` (`text` ,`start` ,`end`  ,`done`)VALUES ('new line', '$new_start', '$new_end', '0');");
arrange_subtitle_lines_order($table_id);
}

if(!empty($delete_x) and !empty($delete_y))
{
mysql_query("DELETE FROM `$table_id` WHERE `id` = $id LIMIT 1");
arrange_subtitle_lines_order($table_id);
is_subtitle_empty($table_id);
}

if(!empty($translate_x) and !empty($translate_y))
{
$res = mysql_query("SELECT * FROM `subtitles` WHERE `table_name` = '$table_id' limit 1");
$row = mysql_fetch_array($res);
$translate_subtitle_id = $row['id'];
$translate_subtitle_info = get_subtitle_info($translate_subtitle_id);
//print_r($translate_subtitle_info);
$lang_from = $translate_subtitle_info['original_language'];
$lang_to = $translate_subtitle_info['language'];
$lang_translate_from =get_language_by_id($lang_from);
$lang_translate_from = $lang_translate_from['lang'];
$lang_translate_to = get_language_by_id($lang_to);
$lang_translate_to = $lang_translate_to['lang'];
$all_languages = get_all_languages();

//https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&q=hello%20world&source=en&target=de
	  $trans = new Google_Translate_API;
	  $translation = $trans->translate($text, $from = $lang_translate_from, $to = $lang_translate_to);
	   
echo "
<center>
<form>
<table>
<tr>
<td><textarea id='translated' name='translated' cols='60' rows='1'>$translation</textarea></td>
</tr>
<tr>
<td>
<center>
<div id='langinfo'>
from <u>".$lang_translate_from."</u>
to <u>".$lang_translate_to."</u>
 <input type='submit' value='Change' onclick=\"self.parent.location='index.php?page=edit_subtitle_info&subtitle_id=".$translate_subtitle_id."'\" />
</div>
</center>
</td>
</tr>
</table>
</form>
</center>
";
}

$subtitle_lines_count = get_subtitle_lines_count($table_id);
//echo $subtitle_lines_count;
?> 

<html>
<head>
	<head>
		<link rel="stylesheet" href="templates/<?php echo read_option('theme_folder');?>/css/editor_style.css" type="text/css" media="all">
		<link type="text/css" href="jquery/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	
		<script type="text/javascript" src="jquery/js/jquery-1.6.2.min.js"></script>
		<script type="text/javascript" src="jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
		<script type="text/javascript">
			$(function(){
				
				// Progressbar
				$("#progressbar").progressbar({
					value: <?php echo get_subtitle_progress($table_id); ?>					
				});		
				
			});
		</script>

	</head>
</head>
<body  name='editor_page' id='editor_page' class='editor_page' onLoad="setHeight()">
<script>
function setHeight() {
    parent.document.getElementById('subteditor').height = document['body'].offsetHeight;
}
</script>

<?php

if ( $table_id!="" and $subtitle_id!="" )
{

$current = $subtitle_id;

if(empty($s) or $s<0) {$s = 0;}
if(empty($prev)) $prev=1;
if(empty($next)) $next=5;

if($current!="") 
{
if(is_valid_subtitle_page_index($table_id, $current-5)=='false'){$prev=1;} else {$prev = $current -5;}
if(is_valid_subtitle_page_index($table_id, $current+5)=='false'){$next = $current;} else {$next = $current +5;}
}

for ($i = $subtitle_id; $i <= $subtitle_id + 4; $i++) {
//echo "SELECT * FROM `$table_id` WHERE `line_number`='$i' LIMIT 1";
$result = mysql_query("SELECT * FROM `$table_id` WHERE `line_number`='$i' LIMIT 1");
$row = mysql_fetch_array($result);

$start = $row['start'];
$end = $row['end'];
$text = $row['text'];
$line_number = $row['line_number'];
$id = $row['id'];
$done = $row['done'];
$translated_text = $row['translated_text']; 

$player_start = convert_srt_time_to_seconds($start) - $it_increase_replay_time;
$player_end = convert_srt_time_to_seconds($end) + $it_increase_replay_time;
if($player_start < 0){$player_start = 0;}

//echo convert_srt_time_to_seconds($start);
//echo $player_start;
//echo convert_srt_time_to_seconds($start);
//echo convert_srt_time_to_seconds_full($start);
//echo convert_seconds_to_srt_time(convert_srt_time_to_seconds_full($start));

if($id!='')
{
echo "
<center>
    <div id='wrapper'>
	<table border='0' cellspacing='0' cellpadding='0' style='border:#000000;'>
	<form name='form".$i."' id='form".$i."' action='get_subtitles.php' method='get'>
	<tr>
	<td style='width:%100'>
	";

if(!empty($saved_id) and $saved_id == $id) echo show_done_image('2');

echo "
	<textarea id='subtitle_text' name='text' rows='2' cols='40' title='".$translated_text."' onClick='select()' >".$text."</textarea>
	</td>
	<td>
	<table>
	<tr><td><INPUT type='text' id='subtitle_start' name='start' value='".$start."' /></td></tr>
	<tr><td><INPUT type='text' id='subtitle_end' name='end' value='".$end."' /></td></tr>
	</table>
	<INPUT type='hidden' name='id' value='".$id."' />	
	<INPUT type='hidden' name='line_number' value='".$line_number."' />
	</td>
	<td>
	<table>
	<tr>
	<input type='hidden' name='table_id' value='".$table_id."' />	
	<input type='hidden' name='subtitle_id' value='".$current."' />
	<input type='hidden' name='current_subtitle_id' value='".$current_subtitle_id."'/>
	<tr>
	<td style='width:24px'><input type='image' id='translate' title='Translate' name ='translate' src='none.png'  /></td>
	<td style='width:24px'><input type='image' id='reply' title='Replay' name ='play' src='none.png' onclick='JavaScript:self.parent.document.getElementById(&quot;epistart&quot;).innerHTML = &quot;".$start."&quot;;self.parent.document.getElementById(&quot;epistop&quot;).innerHTML = &quot;".$end."&quot;;self.parent.stopat=".$player_end.";self.parent.startat=".$player_start.";self.parent.jwplayer().seek(".$player_start.");' /></td>
	<td style='width:24px'><input type='image' id='add_before' title='Add before' name='add_before' src='none.png' /></td>
	<td style='width:24px'><INPUT type='image' id='save' title='Save' src='none.png' name='save' value='save' /></td>
	</tr>
	<tr>
	<td style='width:24px'><img title='Paste Translation' id='paste' name ='paste' src='none.png' onclick=\"document.form".$i.".text.value = document.getElementById('translated').value;\"/></td>
	<td style='width:24px'><input type='image' title='Delete' id='delete' name='delete' src='none.png' /></td>
	<td style='width:24px'><input type='image' title='Add after' id='add_after' name='add_after' src='none.png' /></td>";

if ($done=='0')
{
echo "	<td style='width:24px'><INPUT type='image' title='Done' id='done' src='none.png' name='done' value='done'/></td>";
}
else
{
echo "	<td style='width:24px'><INPUT type='image' title='Done' id='done2' src='none.png' name='not_done' value='not_done'/></td>";
}	
	echo "</tr>
	</table>
	</td>
	</tr>
	</form>
	</table>
	</div>
</center>
	";
}
}
echo "
<br>
<div id='progressbar' style='height:20;'></div>
<br>
<div id='next'>
<center>

<table border='0'>
<tr>
<td>
<form action='get_subtitles.php' method='post' style='display:inline;'>
<INPUT type='image' id='before' src='none.png' />
<input type='hidden' name='table_id' value='".$table_id."' />
<input type='hidden' name='subtitle_id' value='".$prev."' />
<input type='hidden' name='current_subtitle_id' value='".$current_subtitle_id."'/>
</form>
</td>
<td valign='top'>
<form action='get_subtitles.php' method='post' style='display:inline;'>
<select name='subtitle_id' onchange='this.form.submit()'>";
$count = 0;
for ($i = 1; $i <= $subtitle_lines_count; $i++) {
	$count = $count + 1;
	if($subtitle_id==$i){$selected = 'selected=selected';}else{$selected = '';}
    echo "<option value='$i' $selected>page $count</option>";
	$i = $i + 4;
}
echo 
"</select>
<input type='hidden' name='table_id' value='".$table_id."' />
<input type='hidden' name='current_subtitle_id' value='".$current_subtitle_id."'/>
</form>
</td>
<td>
<form action='get_subtitles.php' method='post' style='display:inline;'>
<INPUT type='image' id='after' src='none.png' />
<input type='hidden' name='table_id' value='".$table_id."' />
<input type='hidden' name='subtitle_id' value='".$next."' />
<input type='hidden' name='current_subtitle_id' value='".$current_subtitle_id."'/>
</form>
</td>
</table>

</center>
</div>
	";

	}
	
	
/////////////////////////////////////////////////////////////////////
function show_done_image($seconds)
{

echo "
<div name='fadeimage' id='fadeimage' style='width:24px'>
<img id='saved' src='none.png' />
</div>

<script>
setTimeout(function() {
    $('#fadeimage').fadeOut('fast');
}, ". $seconds * 1000 ."); // <-- time in milliseconds
</script>

";
}
/////////////////////////////////////////////////////////////////////	
?>

</body>
</html>